# -*- coding: utf-8 -*-

import okex.futures_api as future
import time

class arbitrage():
    
    def __init__(self,ACCESS_KEY,SECRET_KEY):
        passphrase = '123456ab'
        self.okk = future.FutureAPI(ACCESS_KEY, SECRET_KEY, passphrase, True)

    def datas(self,pp1,pp2):
        while True:
            try:
                a = self.okk.get_depth(pp1, 1)
                b = self.okk.get_depth(pp2, 1)
                a1 = float(b.get('asks')[0][0])
                a2 = float(a.get('bids')[0][0])
                a3 = float(b.get('bids')[0][0])
                a4 = float(a.get('asks')[0][0])
                dif1 = (a3-a4)/a4
                dif2 = (a1-a2)/a2
                return dif1,dif2
                break
            except:
                time.sleep(2)
                continue
    def openn(self,p1,p2,diff,amount,add,amount_p1,amount_p2,direction):
        print('开仓 ',p1,' && ', p2)
        print(str(diff),str(amount),str(add),str(amount_p2),str(amount_p1))
        # p1='DOT-USD-210326'
        # p2='DOT-USD-210625'        
        if float(p1[-6:]) < float(p2[-6:]) :
            number = 0
            n = int(amount/min(amount_p1,amount_p2)) 
            while True:
                cc=list(self.datas(p1,p2))
                if cc[0]>diff and number<n :
                    s_order = self.okk.take_order(p2, 2,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 1,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                if not add and number == n:
                    print('建仓结束')
                    break
                if add and number == n and direction==1:
                    self.okk.take_order(p2,2,'',add, 1)
                    print('已经加仓'+str(add)+'张--空单')
                    break
                if add and number == n and direction==2:
                    self.okk.take_order(p2,4,'',add, 1)
                    print('已经平仓'+str(add)+'张--空单')
                    break
                time.sleep(1)
        if  float(p1[-6:]) > float(p2[-6:]) :
            number = 0
            n = int(amount/min(amount_p1,amount_p2)) 
            while True:
                cc=list(self.datas(p2,p1))
                if cc[1] <diff and number<n :
                    s_order = self.okk.take_order(p2, 2,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 1,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break

                if not add and number == n:
                    print('建仓结束')
                    break
                if number == n and direction==1:
                    self.okk.take_order(p1,1,'',add, 1)
                    print('已经加仓'+str(add)+'张--多单')
                    break
                if number == n and direction==2:
                    self.okk.take_order(p1,3,'',add, 1)
                    print('已经平仓'+str(add)+'张--多单')
                    break
                time.sleep(1)
                
    def close(self,p1,p2,diff,amount,amount_p1,amount_p2):
        print('平仓 ',p1,' && ', p2)
        print(str(diff),str(amount))
        if float(p1[-6:]) < float(p2[-6:]) :
            number = 0
            n = int(amount/min(amount_p1,amount_p2)) 
            while True:
                cc=list(self.datas(p1,p2))
                if cc[1]<diff and number<n :
                    s_order = self.okk.take_order(p2, 4,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 3,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                if number == n :
                    print('平仓完成')
                    break
                time.sleep(1)
        if float(p1[-6:]) > float(p2[-6:]) :
            number = 0
            n = int(amount/min(amount_p1,amount_p2)) 
            while True:
                cc=list(self.datas(p2,p1))
                if cc[0] > diff and number<n :
                    s_order = self.okk.take_order(p2, 4,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 3,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                if number == n :
                    print('平仓完成')
                    break
                time.sleep(1)
                
    def circle(self,p1,p2,open_dif,close_dif,amount,amount_p1,amount_p2):
        print('单向套利 ',p1,' && ', p2)
        print(str(open_dif),str(close_dif),str(amount))
        if  float(p1[-6:]) < float(p2[-6:]) :
            number = 0
            n = int(amount/min(amount_p1,amount_p2)) 
            while True:
                cc=list(self.datas(p1,p2))
                if cc[0]>open_dif and number<n :
                    s_order = self.okk.take_order(p2, 2,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 1,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                if cc[1]<close_dif and number>0:
                    s_order = self.okk.take_order(p2, 4,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 3,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                time.sleep(1)
        if float(p1[-6:]) > float(p2[-6:]) :
            number = 0
            n = int(amount/min(amount_p1,amount_p2)) 
            while True:
                cc=list(self.datas(p2,p1))
                if cc[1]<open_dif and number<n :
                    s_order = self.okk.take_order(p2, 2,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 1,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                if cc[0]>close_dif and number>0:
                    s_order = self.okk.take_order(p2, 4,'',amount_p2, 1)
                    b_order = self.okk.take_order(p1, 3,'',amount_p1, 1)
                    if float(s_order.get('order_id')) != float(-1)  and float(b_order.get('order_id')) != float(-1) :
                        number += 1
                        print(str(number))
                    else:
                        print('注意：下单失败')
                        print(s_order.get('error_code'),b_order.get('error_code'))
                        print(s_order.get('error_message'),b_order.get('error_message'))
                        break
                time.sleep(1)
                    


